﻿@using AntDesign.Select;
@typeparam TDto
<Row Style="margin-bottom:10px;">
    <AntDesign.Col Span="4">
        @if (_fields.Any())
        {
            <Select Mode="multiple"
                    Placeholder="@localizer[nameof(SharedLocalResource.PleaseSelectSearchField)]"
            @bind-Values="_selectedValues"
            TItemValue="string"
            TItem="string"
            OnSelectedItemsChanged="OnSelectedItemsChangedHandler"
            MaxTagCount="@ResponsiveTag.Responsive"

        AllowClear
             OnClearSelected="OnClearSearchValue">
                 <SelectOptions>
                     @foreach (TableSearchField item in _fields)
                    {
                        <SelectOption TItemValue="string" TItem="string" Value="@item.Name" Label="@item.DisplayName" />
                    }
                </SelectOptions>
        </Select>
        }
    </AntDesign.Col>
    <AntDesign.Col Span="18">
        @if(_fields.Any()){
            string width = Math.Ceiling((decimal)100 / _fields.Count()) + "%";
            string style = $"margin-right:8px;margin-bottom:2px;width:100px";
            string begin = localizer[nameof(SharedLocalResource.Begin)];
            string end = localizer[nameof(SharedLocalResource.End)];
            foreach (TableSearchField fieldInfo in _fields)
            {
                string displayName = fieldInfo.DisplayName;
                string boStyle = "float:left;" + (_selectedValues != null && _selectedValues.Any(x => x.Equals(fieldInfo.Name)) ? "" : "display:none;");
                <div style="@boStyle">
                    <span>@(displayName + ":")</span>
                    @{
                        if (fieldInfo.IsSetSelectItem && fieldInfo.SelectItems != null)
                        {
                            <Select Mode="multiple"
                                    Placeholder="@displayName"
                                    Style="@style"
                                    TItem="string"
                                    TItemValue="string"
                                    OnSelectedItemsChanged="items=>{fieldInfo.Values=items;}"
                                    DefaultValues="fieldInfo.Values"
                                    Values="fieldInfo.Values"
                                    MaxTagCount="@ResponsiveTag.Responsive"
                                            AllowClear>
                             <SelectOptions>
                                 @foreach (KeyValuePair<string,string> item in fieldInfo.SelectItems)
                                    {
                                        <SelectOption TItemValue="string" TItem="string" Value="@item.Key" Label="@item.Value" />
                                    }
                                </SelectOptions>
                            </Select>

                        }
                        else if (fieldInfo.Type.Equals(typeof(string)))
                        {
                            <Input Style="@style" Type="text" OnPressEnter="OnSearchClick" Placeholder="@displayName" TValue="string" @bind-Value="@fieldInfo.Value" />
                        }
                        else if (fieldInfo.Type.GetNonNullableType().Equals(typeof(Guid)))
                        {
                            <Input Style="@style" Type="text" OnPressEnter="OnSearchClick" Placeholder="@displayName" TValue="string" @bind-Value="@fieldInfo.Value" />
                        }
                        else if (fieldInfo.Type.GetNonNullableType().Equals(typeof(int)))
                        {
                            <Input Style="@style" Type="number" OnPressEnter="OnSearchClick" Placeholder="@displayName" TValue="string" @bind-Value="@fieldInfo.Value" />
                        }
                        else if (fieldInfo.Type.GetNonNullableType().Equals(typeof(short)))
                        {
                            <Input Style="@style" Type="number" OnPressEnter="OnSearchClick" Placeholder="@displayName" TValue="string" @bind-Value="@fieldInfo.Value" />
                        }
                        else if (fieldInfo.Type.GetNonNullableType().Equals(typeof(long)))
                        {
                            <Input Style="@style" Type="number" OnPressEnter="OnSearchClick" Placeholder="@displayName" TValue="string" @bind-Value="@fieldInfo.Value" />
                        }
                        else if (fieldInfo.Type.GetNonNullableType().Equals(typeof(decimal)))
                        {
                            <Input Style="@style" Type="number" OnPressEnter="OnSearchClick" Placeholder="@displayName" TValue="string" @bind-Value="@fieldInfo.Value" />
                        }
                        else if (fieldInfo.Type.GetNonNullableType().Equals(typeof(double)))
                        {
                            <Input Style="@style" Type="number" OnPressEnter="OnSearchClick" Placeholder="@displayName" TValue="string" @bind-Value="@fieldInfo.Value" />
                        }
                        else if (fieldInfo.Type.GetNonNullableType().Equals(typeof(DateTimeOffset)) || fieldInfo.Type.GetNonNullableType().Equals(typeof(DateTime)))
                        {
                            DateTime? tempBeginTime = fieldInfo.Values == null ? null : DateTime.Parse(fieldInfo.Values.First());
                            DateTime? tempEndTime = fieldInfo.Values == null ? null : DateTime.Parse(fieldInfo.Values.Last());
                            <RangePicker Style="margin-left:5px;"
                                         TValue="DateTime?[]"
                                         ShowTime="@Settings.ShowTime"
                                         Placeholder="new []{fieldInfo.DisplayName+begin,fieldInfo.DisplayName+end}"
                                         OnChange="timesEvent=>fieldInfo.Values=DateTimeFormat(timesEvent)"
                                         DefaultValue="new DateTime?[] { tempBeginTime, tempEndTime}"
                                         OnClear="()=>{fieldInfo.Values=new string[0];}" />
                        }
                        else if (fieldInfo.Type.GetNonNullableType().Equals(typeof(bool)))
                        {
                            <Select Mode="multiple"
                                    Placeholder="@displayName"
                                    TItemValue="string"
                                    TItem="string"
                                    MaxTagCount="@ResponsiveTag.Responsive"
                                    Style="@style"
                                            AllowClear
                                    OnSelectedItemsChanged="items=>{fieldInfo.Values=items;}"
                                    DefaultValues="fieldInfo.Values"
                                    Values="fieldInfo.Values">
                                <SelectOptions>
                                    <SelectOption TItemValue="string" TItem="string" Value="@("true")" Label="@localizer[nameof(SharedLocalResource.Yes)]" />
                                     <SelectOption TItemValue="string" TItem="string" Value="@("false")" Label="@localizer[nameof(SharedLocalResource.No)]" />
                                </SelectOptions>
                            </Select>
                        }
                        else if (fieldInfo.Type.IsEnum)
                        {
                            <Select Mode="multiple"
                                    Placeholder="@displayName"
                                    Style="@style"
                                    TItemValue="string"
                                    TItem="string"
                                    MaxTagCount="@ResponsiveTag.Responsive"
                                            AllowClear
                                    OnSelectedItemsChanged="items=>{fieldInfo.Values=items;}"
                                    DefaultValues="fieldInfo.Values"
                                    Values="fieldInfo.Values">
                                <SelectOptions>
                                    @foreach (var item in EnumHelper.EnumToDictionary(fieldInfo.Type))
                                    {
                                        if (item.Value != null)
                                        {
                                            <SelectOption TItemValue="string" TItem="string" Value="@item.Key.ToString()" Label="@localizer[item.Value]" />
                                        }
                                    }
                                </SelectOptions>
                            </Select>
                        }
                    }
                </div>
            }
        }
    </AntDesign.Col>
    <AntDesign.Col Span="2" Style="text-align:right">
        @if (Settings.AlwaysShowSearchButton || (_selectedValues != null && _selectedValues.Count() > 0))
        {
            <Button Icon="search" Shape="@ButtonShape.Round" OnClick="OnSearchClick">
                @localizer[nameof(SharedLocalResource.Search)]
            </Button>
        }
    </AntDesign.Col>
</Row>

